<?php
/**
 * 首页
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/23
 * Time: 23:03
 */

namespace app\index\controller;

use app\common\controller\HomeBase;
use think\Cache;
use think\Db;
use base\Url;
use app\common\model\Attachment as AttachmentModel;
use app\common\model\BoolStatus;
use app\common\model\FavoritesItem;

class Index extends HomeBase
{
    /**
     * 推荐商品缓存名称
     * @var string
     */
    private $_cacheRecommendGoods = 'RecommendGoods';

    /**
     * 推荐商品列表缓存名称
     * @var string
     */
    private $_cacheRecommendGoodsList = 'RecommendGoodsList';

    /**
     * 推荐文章列表缓存名称
     * @var string
     */
    private $_cacheRecommendArticleList = 'RecommendArticleList';

    /**
     * 推荐焦点文章缓存名称
     * @var string
     */
    private $_cacheRecommendFocus = 'RecommendFocusArticle';

    /**
     * 缓存时间
     * @var int
     */
    private $_cacheTime = 3600;

    /**
     * 商品模型
     * @var FavoritesItem
     */
    private $_favoritesItemModel = null;


    /**
     * 构造方法
     * Index constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->_favoritesItemModel = new FavoritesItem();
    }

    /**
     * 默认入口
     * @return mixed
     */
    public function index()
    {
        //介绍
        $this->assign('introduceData', $this->_getIntroduce());

        //推荐商品
        $this->assign('recommendGoods', $this->_getRecommendGoods());

        //商品列表
        $this->assign('recommendGoodsList', $this->_getRecommendGoodsList());

        //推荐文章列表
        $this->assign('articleData', $this->_getRecommendArticleList());

        //获取焦点文章
        $this->assign('recommendData', $this->_getRecommendFocus());

        $this->setSEOInfo('首页', '为家乡代言,美食,美景,旅行,购物', '为家乡代言');
        return $this->fetch('index/index');
    }

    /**
     * 介绍
     * @return array
     */
    private function _getIntroduce()
    {
        $introduceData = [
            [
                'title'   => '农作物、经济作物',
                'img'     => 'http://www.dadiba.net/public/uploads/a1.jpg',
                'link'    => url('@index/lists/index', 'id=1'),
                'content' => '农作物、经济作物： 绿色，安全，环保，放心',
            ],
            [
                'title'   => '风景名胜',
                'img'     => 'http://www.dadiba.net/public/uploads/a2.jpg',
                'link'    => url('@index/lists/index', 'id=2'),
                'content' => '对奉节这个名字不熟悉没关系，知道诸葛亮白帝城托孤，“朝辞白帝彩云间”，和世界第一天坑与最长地下暗河——都在这...',
            ],
            [
                'title'   => '时尚女装',
                'img'     => 'http://www.dadiba.net/public/uploads/a3.jpg',
                'link'    => '/shopping.html',
                'content' => '时尚女装，学生装，夏装，连衣裙，长裙，短裙，衬衫，打底裙，t恤，雪纺衫，针织衫，牛仔裤，卫衣，运动服，小西装',
            ],
        ];

        return $introduceData;
    }


    /**
     * 推荐商品
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    private function _getRecommendGoods()
    {
        $goodsData = Cache::get($this->_cacheRecommendGoods);
        if (!$goodsData) {
            $goodsData = $this->_favoritesItemModel->getRandRecommendData(4);
            Cache::set($this->_cacheRecommendGoods, $goodsData, $this->_cacheTime);
        }
        return $goodsData;
    }

    /**
     * 推荐商品列表
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    private function _getRecommendGoodsList()
    {
        $recommendGoodsList = Cache::get($this->_cacheRecommendGoodsList);
        if (!$recommendGoodsList) {
            $recommendGoodsList = $this->_favoritesItemModel->getRandRecommendData(8);
            Cache::set($this->_cacheRecommendGoodsList, $recommendGoodsList, $this->_cacheTime);
        }
        return $recommendGoodsList;
    }

    /**
     *  推荐文章
     * @return array|mixed
     */
    private function _getRecommendArticleList()
    {
        $articleData = Cache::get($this->_cacheRecommendArticleList);
        if (!$articleData) {
            $aidArr = [6, 2, 3];   //文章
            $list   = Db::view('article')
                ->view('article_detail', 'seo_desc', 'article.id=article_detail.id', 'left')
                ->view('attachment', 'attachment as img', 'article.id=attachment.tid and attachment.type="' . AttachmentModel::ARTICLE . '"', 'left')
                ->view('user', 'name as author', 'article.uid=user.id', 'left')
                ->where('article.status', '=', BoolStatus::OPEN)
                ->where('article.id', 'in', $aidArr)
                ->order('article.id', 'desc')
                ->select();

            $articleData = [];
            foreach ($list as $_item) {
                $_item['img']  = $_item['img'] ? Url::getRemoteImgSrc($_item['img']) : '';
                $articleData[] = $_item;
            }
            Cache::set($this->_cacheRecommendArticleList, $articleData, $this->_cacheTime);
        }
        return $articleData;
    }

    /**
     * 获取焦点文章
     * @return array|mixed
     */
    private function _getRecommendFocus()
    {
        $recommendData = Cache::get($this->_cacheRecommendFocus);
        if (!$recommendData) {
            $aidArr = [8, 9, 11];   //焦点
            $list   = Db::view('article')
                ->view('article_detail', 'seo_desc', 'article.id=article_detail.id', 'left')
                ->view('attachment', 'attachment as img', 'article.id=attachment.tid and attachment.type="' . AttachmentModel::ARTICLE . '"', 'left')
                ->view('user', 'name as author', 'article.uid=user.id', 'left')
                ->where('article.status', '=', BoolStatus::OPEN)
                ->where('article.id', 'in', $aidArr)
                ->order('article.id', 'desc')
                ->select();

            $recommendData = [];
            foreach ($list as $_item) {
                $_item['img']    = $_item['img'] ? Url::getRemoteImgSrc($_item['img']) : '';
                $recommendData[] = $_item;
            }
            Cache::set($this->_cacheRecommendFocus, $recommendData, $this->_cacheTime);
        }
        return $recommendData;
    }
}